import { Injectable } from '@angular/core';
import { AuthenticationService } from './service/auth.service';
import {
    CanActivate, Router,
    ActivatedRouteSnapshot,
    RouterStateSnapshot
} from '@angular/router';

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(
        private authService: AuthenticationService,
        private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        let url: string = state.url;
        console.log("canActivate", url);
        return this.checkLogin(url);
    }

    checkLogin(url: string): boolean {
        if (this.authService.IsLoginIn) { return true; }
        //TODO:

        // Store the attempted URL for redirecting
        //this.authService.redirectUrl = url;

        // Navigate to the login page with extras
        this.router.navigate(['/user/1']);
        return false;
    }
}